package com.lzx.iteam;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.*;
import com.lzx.iteam.adapter.SelectContactAdapter;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.CRMBaseBean;
import com.lzx.iteam.bean.CRMLibraryListData;
import com.lzx.iteam.bean.ScheduleManager;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.CRMChanceListMsg;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.util.PreferenceUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

import static com.baidu.location.f.mC;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 08月 11日
 * Time: 下午2:56
 *
 * @QQ : 1234567890
 * **********************************
 */
public class CRMSelectContactActivity extends MyBaseActivity implements AdapterView.OnItemClickListener {

    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.crm_edit_list)
    private ListView mListView;

    /**
     * 操作类型:
     * 1: 联系人
     * 2: 转移联系人
     */
    private int mFlag;

    /**
     * 所属类型:
     * 0.公海(默认)
     * 1.线索库
     * 2.机会库
     * 3.客户库
     */
    private int mType;
    private String mGroupId;
    private String mChanceId;
    private SelectContactAdapter mAdaper;
    private ArrayList<CRMLibraryListData> mListData = new ArrayList<CRMLibraryListData>();

    private final int MSG_TRANSFER_CHANCE = 1000;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_TRANSFER_CHANCE:
                    if (msg.arg1 == 0) {
                        Toast.makeText(mActivity, "此机会已成功转给他人", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(mActivity, CRMLibraryActivity.class);
                        intent.putExtra("type", mType);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(mActivity, (String) msg.obj,
                                Toast.LENGTH_LONG).show();
                    }
                    break;
            }
        }
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.crm_edit_layout);
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mListView.setOnItemClickListener(this);

    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        mFlag = intent.getIntExtra("flag", 0);
        mType = intent.getIntExtra("type", 0);
        mChanceId = intent.getStringExtra("chance_id");
        mGroupId = intent.getStringExtra("group_id");
        if (mFlag == 1) {
            mTvTitle.setText("联系人");
            mListData = (ArrayList<CRMLibraryListData>) intent.getSerializableExtra("clues");
            mAdaper = new SelectContactAdapter(mActivity);
            mListView.setAdapter(mAdaper);
            mAdaper.bindData(1, mListData);
        } else if (mFlag == 2) {
            mTvTitle.setText("选择同事");
            /**
             * 暂时固定为转给张帅:
             */
            if("381712".equals(mPreferenceUtil.getString(PreferenceUtil.CLIENT_USEID, "－1"))){
                transferChanceMsg("426402");
            }else if("426402".equals(mPreferenceUtil.getString(PreferenceUtil.CLIENT_USEID, "－1"))){
                transferChanceMsg("381712");
            }
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if (mFlag == 1) {
            Intent clue = new Intent(mActivity, CRMContactActivity.class);
            clue.putExtra("type", 0);
            clue.putExtra("clue_id", mListData.get(position).get_id());
            clue.putExtra("group_id", mGroupId);
            startActivity(clue);
        }

    }

    private void transferChanceMsg(String userId) {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
                .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_CHANCE_ID, mChanceId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_DEST_USER_ID, userId));

        CRMChanceListMsg msg = new CRMChanceListMsg(mHandler.obtainMessage(MSG_TRANSFER_CHANCE));
        msg.mApi = AsynHttpClient.API_CRM_CHANCE_TRANSFER;
        msg.mParams = nvps;
        AsynHttpClient.getInstance(this).execCRMHttp(msg);

    }


    @Override
    public void onClick(View v) {

    }
}
